package HashMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class HomeWork08 {
	public static void main(String[] args) {
		//准备牌
				ArrayList<String> list=new ArrayList<String>();
				Collections.addAll(list, "0-大王","1-小王","2-♥2","3-♣2","4-♦2","5-♠2","6-♥A","7-♣A","8-♦A","9-♠A",
"10-♥K","11-♣K","12-♦K","13-♠K","14-♥Q","15-♣Q","16-♦Q","17-♠Q","18-♥J","19-♣J","20-♦J","21-♠J","22-♥10","23-♣10","24-♦10","25-♠10","26-♥9","27-♣9","28-♦9","29-♠9",
"30-♥8","31-♣8","32-♦8","33-♠8","34-♥7","35-♣7","36-♦7","37-♠7","38-♥6","39-♣6","40-♦6","41-♠6","42-♥5","43-♣5","44-♦5","45-♠5","46-♥4","47-♣4","48-♦4","49-♠4",
"50-♥3","51-♣3","52-♦3","53-♠3");
				HashMap<Integer,String> map = new HashMap();
				for (String s : list){
					String[] spl = s.split("-");
					if (spl.length == 2){
						Integer num = Integer.parseInt(spl[0]);
						String card = spl[1];
						map.put(num, card);
					}
				}
				//洗牌
				// 将HashMap转换为List
		        List<Map.Entry<Integer, String>> entryList = new ArrayList<>(map.entrySet());

		        // 随机排序List
		        Collections.shuffle(entryList);

		        // 创建新的LinkedHashMap并将排序后的元素放入
		        LinkedHashMap<Integer, String> shuffleMap = new LinkedHashMap<>();
		        for (Map.Entry<Integer, String> entry : entryList) {
		        	shuffleMap.put(entry.getKey(), entry.getValue());
		        }
				Player playe1=new Player("老王");
				Player playe2=new Player("老李");
				Player playe3=new Player("老刘");
				//摸牌
				int j = 0;
				for (int i :shuffleMap.keySet()){
					j++;
					if(j<=51){
						if(j%3==0){
							playe1.getList().add(shuffleMap.get(i));
						}else if(j%3==1){
							playe2.getList().add(shuffleMap.get(i));
						}else{
							playe3.getList().add(shuffleMap.get(i));
						}
					}
				}
				//看牌
				// 对玩家手中的牌进行排序
				LinkedHashMap<Integer, String> sortedPlayer1 = new LinkedHashMap<>();
				LinkedHashMap<Integer, String> sortedPlayer2 = new LinkedHashMap<>();
				LinkedHashMap<Integer, String> sortedPlayer3 = new LinkedHashMap<>();

				List<Integer> sortedKeys = new ArrayList<>(shuffleMap.keySet());
				Collections.sort(sortedKeys);

				for (int key : sortedKeys) {
				    if (playe1.getList().contains(shuffleMap.get(key))) {
				    	//LinkedHashMap是保持插入顺序的一种方法
				        sortedPlayer1.put(key, shuffleMap.get(key));
				    } else if (playe2.getList().contains(shuffleMap.get(key))) {
				        sortedPlayer2.put(key, shuffleMap.get(key));
				    } else if (playe3.getList().contains(shuffleMap.get(key))) {
				        sortedPlayer3.put(key, shuffleMap.get(key));
				    }
				}
				
				//values()方法来获取LinkedHashMap中所有值的集合。
				System.out.println(playe1.getName() + " : " + sortedPlayer1.values());
				System.out.println(playe2.getName() + " : " + sortedPlayer2.values());
				System.out.println(playe3.getName() + " : " + sortedPlayer3.values());
	}
}
